package com.hmys.api.service;

import com.hmys.api.mapper.WebUserMapper;

import com.hmys.api.model.Principal;
import com.hmys.api.model.WebUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Created by philip on 5/19/17.
 */
@Service
public class AuthenticationService implements UserDetailsService {

    @Autowired
    private WebUserMapper userMapper;

    public UserDetails findUserByUserId(Long id) throws UsernameNotFoundException {
        WebUser user = userMapper.selectByPrimaryKey(id);
        if (user == null)
            throw new UsernameNotFoundException("用户不存在");
        return new Principal(user);
    }

    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        WebUser user = userMapper.selectByPhone(s);
        if (user == null)
            throw new UsernameNotFoundException("用户" + s + "不存在");
        return new Principal(user);
    }

    public UserDetails loadUserByPhone(String s) throws UsernameNotFoundException {
        WebUser user = userMapper.selectByPhone(s);
        if (user == null)
            throw new UsernameNotFoundException("用户" + s + "不存在");
        return new Principal(user);
    }
}
